*******************************************************************************
cd E:\data\cooperation\govern_patent\data1

***load and deal with the data
use final_3rd,clear
*****add the labels
label var pat_uspt "patent grants"
label var wdi_trade "The sum of exports and imports of goods and services (% of GDP)"
label var lpop_dnst "Log of Population density (people per sq. km of land area)"
label var wbgi_pse "Political Stability"
label var fh_ipolity2 "Level of Democracy (Freedom House/Imputed Polity)"
label var hf_efiscore "Economic Freedom Index"
label var lgdppc "Log of GDP per capita, PPP (constant 2005 international $)"
label var vdem_corr "Political corruption"
label var fe_cultdiv "Cultural Diversity"
label var wdi_telephone "Telephone lines (per 100 people)"
label var ciri_polpris "Political Imprisonment"
label var taxburden "Tax burden"
label var wdi_internetserv "Secure Internet servers (per 1 million people)"
des

use original,clear 
label var pat_uspt "patent grants"
label var wdi_trade "The sum of exports and imports of goods and services (% of GDP)"
label var lpop_dnst "Log of Population density (people per sq. km of land area)"
label var wbgi_pse "Political Stability"
label var fh_ipolity2 "Level of Democracy (Freedom House/Imputed Polity)"
label var hf_efiscore "Economic Freedom Index"
label var lgdppc "Log of GDP per capita, PPP (constant 2005 international $)"
label var vdem_corr "Political corruption"
label var fe_cultdiv "Cultural Diversity"
label var wdi_telephone "Telephone lines (per 100 people)"
label var ciri_polpris "Political Imprisonment"
label var taxburden "Tax burden"
label var wdi_internetserv "Secure Internet servers (per 1 million people)"
des 
 
 
**the main analysis***
egen id = group(countrycode)
gen lpat_uspt=ln(1+pat_uspt)
gen region1=1
replace region1=2 if regioncode==1|regioncode==3|regioncode==5
replace region1=3 if regioncode==4|regioncode==6|regioncode==7
gen lpat_2=lpat_uspt^2
xtset id year
/*1.statisitcal description*/
sum wbgi_gee lpat_uspt wdi_trade lpop_dnst wbgi_pse  fh_ipolity2 hf_efiscore ///
lgdppc vdem_corr fe_cultdiv wdi_telephone ciri_polpris taxburden wdi_internetserv /*table 2*/

/*collin test*/
collin lpat_uspt wdi_trade lpop_dnst wbgi_pse  fh_ipolity2 hf_efiscore ///
lgdppc vdem_corr fe_cultdiv wdi_telephone ciri_polpris taxburden wdi_internetserv /*table 3*/
	
****simple regression***
reg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris wdi_internetserv,robust
 est store reg1
 *****add the square of lpat_uspt****
 reg wbgi_gee lpat_uspt lpat_2 wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris wdi_internetserv,robust
 est store reg4
 *****flgs****
 xtgls wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris wdi_internetserv i.year , panels(hetero)
est store reg2
reg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore ///
vdem_corr fe_cultdiv wdi_telephone taxburden ciri_polpris wdi_internetserv i.regioncode
 est store reg3
esttab  reg1 reg4 reg2 reg3, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(0 3) label(N R-squared ))  /*table 4*/

**********fe model***********************
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, fe
est store fe
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, re
est store re
xtreg wbgi_gee L.lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr, fe
est store fe1
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2 hf_efiscore vdem_corr i.year, fe
est store fe2
esttab  fe re fe1 fe2, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(0 3) label(N R-squared ))  /*table 5*/
  
hausman fe re,sigmamore
hausman fe1 fe,sigmamore
hausman fe2 re,sigmamore

  
*********democracy analysis*****
use final_3rd_demo,clear
egen id = group(countrycode)
gen lpat_uspt=ln(1+pat_uspt)  
xtset id year
preserve
**high level
keep if group==1
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  hf_efiscore vdem_corr,fe
est store high_demo
esttab  high_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))
restore
preserve
******low level
keep if group==0
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  hf_efiscore vdem_corr,fe
est store low_demo
esttab  low_demo , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))
restore
gen group_lpat_uspt=group*lpat_uspt
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse  hf_efiscore vdem_corr i.year,fe
est store all_demo
xtreg wbgi_gee lpat_uspt group_lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse hf_efiscore vdem_corr i.year,fe
est store all1_demo
esttab  all_demo all1_demo, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))

***************robust test*************
**add more variabls
use final_3rd,clear
egen id = group(countrycode)
gen lpat_uspt=ln(1+pat_uspt)
xtset id year 
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr i.year, fe
est store fe
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr wdi_telephone ciri_polpris i.year, fe
est store fe1
esttab  fe fe1, cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))

*******original data
use original,clear
egen id = group(countrycode)
gen lpat_uspt=ln(1+pat_uspt)
xtset id year 
xtreg wbgi_gee lpat_uspt wdi_trade lgdppc lpop_dnst wbgi_pse fh_ipolity2  hf_efiscore vdem_corr i.year, fe
est store fe
esttab  fe , cells(b(star fmt(4)) se(par fmt(2))) /// 
legend label varlabels(_cons constant) /// 
stats(N r2_a, fmt(1 3) label(Observations Adj-R-Square))

